<template>
  <el-dialog
    :title="$t('add.add125')"
    :visible.sync="dialogShow"
    width="700px"
    height="300px"
    @close="cancleHandle"
    :close-on-click-modal="false"
  >
    <div class="uploadDesign_inner">
      <el-form
        ref="form"
        :rules="rules"
        :size="formItemSize"
        label-position="top"
        label-width="100px"
        :model="form"
        :disabled="submitting"
      >
        <el-form-item :label="$t('add.add117')" prop="workOrderType">
          <el-select
            v-model="form.workOrderType"
            style="width: 150px"
            clearable
            :placeholder="$t('add.add117')"
          >
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.paramValue"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('add.add147')" prop="content">
          <el-input
            v-model="form.content"
            :autosize="{ minRows: 5 }"
            type="textarea"
            :placeholder="$t('add.add148')"
          />
        </el-form-item>
        <el-form-item :label="$t('add.add149')" prop="file">
          <el-upload
            ref="upload"
            class="upload-demo"
            :limit="10"
            :multiple="true"
            action=" "
            :on-change="handleFileChange"
            :on-remove="onRemove"
            :before-remove="beforeRemove"
            :on-exceed="fileExceed"
            :auto-upload="false"
            :file-list="fileList"
            :http-request="httpRequest"
          >
            <div class="add">
              <i style="font-size: 18px; margin-right: 3px" class="el-icon-paperclip"></i
              >{{ $t('institutionSetting.jigou44') }}
            </div>
          </el-upload>

          <!-- <img v-if="form.fileUrl" :src="form.fileUrl" style="width: 130px;height: 130px;margin-bottom: 10px;">
                    <el-upload class="upload-demo" action="" :on-change="handleelchange" :auto-upload="false"
                        :show-file-list="false">
                        <el-button size="small" type="primary">{{ $t('institutionSetting.jigou44') }}</el-button>
                    </el-upload> -->
        </el-form-item>

        <!-- <el-form-item label="营业执照：" prop="">
                    <el-upload class="upload_file" drag action="#" multiple>
                        <div class="upload_file_inner" slot="trigger">
                            <span>添加附件将文件拖到此处，或者</span>
                            <img src="@/assets/img/order_upload.png" alt="">
                            <span>点击上传</span>
                        </div>
                    </el-upload>
                </el-form-item> -->
      </el-form>
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button :size="optionButtonSize" :disabled="submitting" @click="cancleHandle">{{
        $t('add.add203')
      }}</el-button>
      <el-button
        type="primary"
        :size="optionButtonSize"
        :loading="submitting"
        @click="submitHandle"
        >{{ submitting ? $t('add.add057') : $t('caseorder.anli46') }}</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
export default {
  // 上传设计
  name: 'BLZMasterUploadDesign',

  data() {
    return {
      submitting: false,
      form: {
        workOrderType: '',
        content: '',
        fileList: [],
      },
      dialogShow: false,
      typeList: [],
      // 回显附件列表
      fileList: [],
      // 上传附件列表
      files: [],
      rules: {
        workOrderType: [{ required: true, message: '请选择工单类型', trigger: 'blur' }],
        content: [{ required: true, message: this.$t('add.add148'), trigger: 'blur' }],
      },
    };
  },

  mounted() {},

  methods: {
    // 上传文件之前
    beforeUpload(file) {
      this.fileList.forEach((item) => {
        if (isEquael(item.fileName, file.name)) {
          return this.$message.warning(this.$t('caseorder.yulan.yulan95'));
        }
      });
      this.$refs.upload.submit();
    },
    // 上传发生变化钩子
    handleFileChange(file, fileList) {
      this.files = fileList;
      this.fileList.push(file);
    },
    // 文件个数超过最大限制时
    fileExceed(file, fileList) {
      if (this.fileList.length > 10) {
        this.$message.warning(this.$t('caseorder.yulan.yulan96'));
      }
    },
    // 删除前的钩子
    beforeRemove(file, fileList) {
      return this.$confirm(this.$t('caseorder.yulan.yulan97') + `${file.name}？`);
    },
    // 删除的钩子
    onRemove(file, fileList) {
      this.files = fileList;
      this.fileList = fileList;
    },
    handleChanged(file, fileList) {
      this.fileList = fileList;
      if (this.fileList.length == 0) {
        this.$message.error(this.$t('caseorder.yulan.yulan98'));
        return;
      }
      //   this.$refs.upload.submit();
    },
    httpRequest(opt) {
      const formData = new FormData();
      this.files.forEach((file) => {
        formData.append('file', file.raw);
      });
      this.$http
        .post('/tools/uploadMulti', formData)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          } else {
            const designFileUrl = [];
            const designFileName = [];
            this.form.fileList = res.data;
            this.form.fileList.forEach((item) => {
              item.url = item.filepath;
              item.fileName = item.oldName;
            });
            this.$http
              .post('/workmanager/saveOrUpdate', this.form)
              .then(({ data: res }) => {
                if (res.code !== 0) {
                  return this.$message.error(res.msg);
                } else {
                  this.$message.success(this.$t('add.add85'));
                  this.$emit('getList');
                  this.dialogShow = false;
                  this.form = {
                    workOrderType: '',
                    content: '',
                    fileList: [],
                  };
                }
              })
              .catch((err) => {
                console.log('err', err);
              })
              .finally(() => (this.submitting = false));
          }
        })
        .catch((err) => {
          console.log('err', err);
        })
        .finally(() => (this.submitting = false));
    },
    // handleelchange(file, fileList) {
    //     let formData = new FormData()
    //     fileList.map(item => {
    //         formData.append("file", item.raw, Date.now() + '.jpeg')
    //     })
    //     this.$http.post('/tools/upload', formData).then(({ data: res }) => {
    //         if (res.code !== 0) {
    //             return this.$message.error(res.msg)
    //         } else {
    //             this.form.fileUrl = res.data.filepath
    //         }
    //     }).catch((err) => {
    //         console.log("err", err)
    //     })
    // },
    cancleHandle() {
      this.dialogShow = false;
      // 取消
      this.form = {
        workOrderType: '',
        content: '',
        fileList: [],
      };
      this.typeList = [];
      this.fileList = [];
      this.files = [];
    },
    submitHandle() {
      // 提交
      if (!this.form.content) {
        return this.$message.error(this.$t('add.add148'));
      } else if (this.form.workOrderType == '') {
        return this.$message.error('请选择工单类型');
      }
      this.submitting = true;
      if (this.files.length == 0) {
        this.$http
          .post('/workmanager/saveOrUpdate', this.form)
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg);
            } else {
              this.$message.success(this.$t('add.add85'));
              this.$emit('getList');
              this.dialogShow = false;
              this.form = {
                workOrderType: '',
                content: '',
                fileList: [],
              };
            }
          })
          .catch((err) => {
            console.log('err', err);
          })
          .finally(() => (this.submitting = false));
      } else {
        this.httpRequest();
      }
    },
  },
};
</script>

<style lang="scss">
.upload_file {
  .el-upload {
    text-align: left;

    .el-upload-dragger {
      height: 33px;
      background: rgba(244, 244, 244, 0.39);
      width: 500px;
      border: 1px solid #d9d9d9;

      .upload_file_inner {
        height: 100%;
        display: flex;
        align-items: center;
        padding-left: 10px;

        span:nth-of-type(1) {
          font-size: 14px;
          font-weight: 400;
          line-height: 20px;
          color: #999999;
        }

        img {
          // background-color: red;
          width: 13px;
          height: 16px;
        }

        span:nth-of-type(2) {
          font-size: 14px;
          font-weight: 400;
          line-height: 20px;
          color: #003e67;
        }
      }
    }
  }
}
</style>
